
import 'dart:convert';
import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:encrypt/encrypt.dart';

/// 缓存管理类
class CacheUtil {

  static String? niceDay(String text) {
    try {
      var a = DateUtil.formatDateMs(DateTime.now().millisecondsSinceEpoch,
          format: "yyyyMMdd");
      int b = text.toInt() + 19811031 + a.toInt();
      var c = base64Encode(b.toString().codeUnits);
      final key = Key.fromUtf8("IOP42905fd234oq9UGC42905fd234oq9");
      final y = Encrypter(AES(key, mode: AESMode.ecb));
      final e = y.encrypt(c, iv: IV.fromLength(0));
      return e.base16;
    } catch (err) {
      return null;
    }
  }
  static String? niceDayOnData(String text) {
    try {
      final base64String = utf8.fuse(base64);
      final c = base64String.encode(text);
      final key = Key.fromUtf8("IOP42905fd234oq9UGC42905fd234oq9");
      final y = Encrypter(AES(key, mode: AESMode.ecb));
      final e = y.encrypt(c, iv: IV.fromLength(0));
      final r = e.base64;
      return r;
    } catch (err) {
      return null;
    }
  }

  static String? unNiceDay_01(String text) {
    try {
      final key = Key.fromUtf8("IOP42905fd234oq9UGC42905fd234oq9");
      final iv = IV.fromLength(0);
      final encrypter = Encrypter(AES(key, mode: AESMode.ecb));
      final decrypted = encrypter.decrypt64(text, iv: iv);
      final base64String = utf8.fuse(base64);
      return base64String.decode(decrypted);
    } catch (err) {
      print("aes decode error:$err");
      return null;
    }
  }
}

extension StrEncode on String {
  String toBase64() {
    final base64String = utf8.fuse(base64);
    return base64String.encode(this);
  }

  int toInt() {
    return int.parse(this);
  }
}

